package com.shopx.config.service.impl;


import com.shopx.base.constants.ResultCodeEnum;
import com.shopx.base.exception.ShopxException;
import com.shopx.base.service.ISysSeqService;
import com.shopx.cmm.DTO.PageDTO;
import com.shopx.config.bean.SysHolidays;
import com.shopx.config.dao.db1.SysHolidaysDao;
import com.shopx.config.model.SysHolidaysModel;
import com.shopx.config.service.ISysHolidaysService;
import com.shopx.core.utils.DateTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * TODO
 *
 * @author lituo
 * @className: SysHolidays
 * @date 2024-08-13
 */
@Service
@Slf4j
public class SysHolidaysServiceImpl implements ISysHolidaysService {

    @Autowired
    private SysHolidaysDao sysHolidaysDao;

    @Resource
    private ISysSeqService sysSeqService;

    @Override
    public SysHolidaysModel selectByPrimaryKey(SysHolidays sysHolidays) {
        if (sysHolidays == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        return sysHolidaysDao.selectByPrimaryKey(sysHolidays);
    }

    @Override
    public int insertSelective(SysHolidays sysHolidays) {
        if (sysHolidays == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        String id = sysSeqService.getTabSeq("SysHolidays", 4, null);
        sysHolidays.setShId(id);
        int result = sysHolidaysDao.insertSelective(sysHolidays);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    public int updateByPrimaryKeySelective(SysHolidays sysHolidays) {
        if (sysHolidays == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = sysHolidaysDao.updateByPrimaryKeySelective(sysHolidays);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    public int deleteByPrimaryKey(SysHolidays sysHolidays) {
        if (sysHolidays == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = sysHolidaysDao.deleteByPrimaryKey(sysHolidays);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    public int selectCount(SysHolidaysModel sysHolidays) {
        return sysHolidaysDao.selectCount(sysHolidays);
    }

    @Override
    public List<SysHolidaysModel> selectList(SysHolidaysModel sysHolidays) {
        List<SysHolidaysModel> list = sysHolidaysDao.selectList(sysHolidays);
        return list;
    }

    @Override
    public PageDTO<SysHolidaysModel> getListPage(SysHolidaysModel sysHolidays) {
        if (sysHolidays == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        PageDTO<SysHolidaysModel> pageDTO = new PageDTO<SysHolidaysModel>();
        pageDTO.setiTotalDisplayRecords(selectCount(sysHolidays));
        sysHolidays.setBegDt(DateTimeUtil.getDate8());
        pageDTO.setData(selectList(sysHolidays));
        return pageDTO;
    }

    @Override
    public String calWorkDay(String beginDay, Integer day) throws Exception {
        String curDt = DateTimeUtil.getDate8();
        List<String> holidaysList = new ArrayList<>();
        List<String> workList = new ArrayList<>();
        //节假日
        SysHolidaysModel sysHolidays = new SysHolidaysModel();
        sysHolidays.setBegDt(curDt);
        List<SysHolidaysModel> daysList = sysHolidaysDao.selectList(sysHolidays);
        for (SysHolidaysModel model : daysList) {
            if ("1".equals(model.getShType())) {
                holidaysList.add(model.getShDay());
            } else if ("2".equals(model.getShType())) {
                workList.add(model.getShDay());
            }
        }

        //测试数据 开始日期
        while (0 != day) {
            String ifdate = dateAddOne(beginDay);
            if (isWorkingDay(ifdate, holidaysList, workList)) {
                day = day - 1;
            }
            beginDay = ifdate;
        }
        beginDay = DateTimeUtil.dateCalc(beginDay, null, "D", -1);
        return beginDay;
    }

    public static void main(String[] args) throws Exception {
        SysHolidaysServiceImpl shs = new SysHolidaysServiceImpl();
        String s = shs.calWorkDay("20240401", 10);
        System.out.println(s);
    }


    /**
     * 验证是否为工作日
     *
     * @param beginDay
     * @param holiday
     * @param extraWorkDay
     * @return
     * @throws Exception
     */
    private Boolean isWorkingDay(String beginDay, List<String> holiday, List<String> extraWorkDay) throws Exception {
        //是否加班日
        if (extraWorkDay.contains(beginDay)) {
            return true;
        }
        //是否节假日
        if (holiday.contains(beginDay)) {
            return false;
        }
        //跳过星期6和星期天
        String week = getWeek(beginDay);
        if ("Saturday".equals(week) || "Sunday".equals(week)) {
            return false;
        }
        return true;

    }

    /**
     * 计算星期几
     *
     * @param dateParm
     * @return
     */
    private String getWeek(String dateParm) {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
        Calendar calendar = Calendar.getInstance();
        try {
            Date date = dateFormat.parse(dateParm);
            calendar.setTime(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String[] weeks = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

        int weekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (weekIndex < 0) {
            weekIndex = 0;
        }
        return weeks[weekIndex];
    }

    /**
     * 日期加+1天
     *
     * @param param
     * @return
     * @throws Exception
     */
    private String dateAddOne(String param) throws Exception {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new SimpleDateFormat("yyyyMMdd").parse(param));
        calendar.add(calendar.DATE, 1);
        Date date = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date);
    }

}

